<template>
  <div>
    <el-dialog :title="curid?'修改':'添加'" :visible.sync="obj.isshow">
      <el-form :model="form">
        <el-form-item label="活动名称" :label-width="formLabelWidth">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动期限" :label-width="formLabelWidth">
          <el-date-picker
            v-model="value2"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            @change="seleTime"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="一级分类" :label-width="formLabelWidth">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择一级分类"
            @change="secand"
          >
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="item in firstArr"
              :key="item.id"
              >{{ item.catename }}</el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" :label-width="formLabelWidth">
          <el-select
            v-model="form.second_cateid"
            placeholder="请选择活动区域"
            @change="goodid"
          >
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="item in secandArr"
              :key="item.id"
              >{{ item.catename }}</el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="商品" :label-width="formLabelWidth">
          <el-select v-model="form.goodsid" placeholder="请选择二级分类">
            <el-option
              :label="item.goodsname"
              :value="item.id"
              v-for="item in goodArr"
              :key="item.id"
              >{{ item.goodsname }}</el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            :active-value="1"
            :inactive-value="2"
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="canle">取 消</el-button>
        <el-button type="primary" v-if="curid==''" @click="addSeck">添 加</el-button>
        <el-button type="primary" v-else @click="updatSeck">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { cateList, goodslist, seckadd, seckinfo,seckedit } from "../../request/api.js";
import { successMsg, failedMsg } from "../../tools/tools.js";

export default {
  props: ["obj"],
  data() {
    return {
      curid:'',
      firstArr: [],
      secandArr: [],
      goodArr: [],
      goodsArr: [],
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      },
      value2: "",
      // value2: [new Date(2000, 10, 10, 10, 10), new Date(2000, 10, 11, 10, 10)],
      formLabelWidth: "120px",
      y:0,
      m:0,
      d:0,
      h:0,
      mm:0,
      yy:0,
      mm:0,
      dd:0,
      hh:0,
      mmmm:0,
    };
  },
  methods: {
    updatSeck(){
      let obj ={
        id : this.curid,
        ...this.form
      }
      seckedit(obj).then(d=>{
        if(d.data.code == 200){
          successMsg('修改成功')
          this.empty()
          this.$emit('updataSeckk')
        }else{
          failedMsg('修改失败')
        }
      })
    },
    add0(m) {
      return m < 10 ? "0" + m : m;
    },
    format(shijianchuo) {
      //shijianchuo是整数，否则要parseInt转换
      var time = new Date(shijianchuo);
      this.y = time.getFullYear();
      this.m = time.getMonth() + 1;
      this.d = time.getDate();
      this.h = time.getHours();
      this.mm = time.getMinutes();
      return 
    },
    format2(shijianchuo) {
      //shijianchuo是整数，否则要parseInt转换
      var time = new Date(shijianchuo);
      this.yy = time.getFullYear();
      this.mm = time.getMonth() + 1;
      this.dd = time.getDate();
      this.hh = time.getHours();
      this.mmmm = time.getMinutes();
      return 
    },
    getOneSeck(id) {
      this.curid = id
      seckinfo({ id }).then((d) => {
        this.format(Number(d.data.list.begintime))
        this.format2(Number(d.data.list.endtime))
        if (d.data.code == 200) {
          this.form = d.data.list;
          this.value2 = [
            new Date(this.y,this.m,this.d,this.h,this.mm),
            new Date(this.yy,this.mm,this.dd,this.hh,this.mmmm),
          ];
        }
      });
    },
    canle() {
      this.empty();
      this.$emit("canle");
      this.curid=''
      this.value2=""
    },
    addSeck() {
      seckadd(this.form).then((d) => {
        console.log(d);
        if (d.data.code == 200) {
          successMsg("添加成功");
          this.$emit("addSeck");
          this.empty();
        } else {
          failedMsg("添加失败");
        }
      });
    },
    goodid() {
      // this.goodArr = this.goodsArr
      let arr = [];
      this.goodsArr.map((item) => {
        if (item.second_cateid == this.form.second_cateid) {
          arr.push(item);
        }
        this.goodArr = arr;
      });
    },
    secand() {
      this.secandArr = this.firstArr.find(
        (item) => item.id == this.form.first_cateid
      ).children;
    },
    seleTime(d) {
      this.form.begintime = d[0];
      this.form.endtime = d[1];
    },
    empty() {
      this.form = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      };
    },
  },
  mounted() {
    cateList().then((d) => {
      if (d.data.code == 200) {
        this.firstArr = d.data.list;
      }
    });
    goodslist().then((d) => {
      if (d.data.code == 200) {
        this.goodsArr = d.data.list;
      }
    });
  },
};
</script>

<style>
.el-date-editor--datetimerange.el-input,
.el-date-editor--datetimerange.el-input__inner {
  width: 100%;
}
</style>